package cn.dnui_yxk801.dnuifood_16110100801.model;

import java.util.List;

import cn.dnui_yxk801.dnuifood_16110100801.bean.ResultBean;
import cn.dnui_yxk801.dnuifood_16110100801.bean.UserBean;
import cn.dnui_yxk801.dnuifood_16110100801.bean.CollectInfoBean;
import cn.dnui_yxk801.dnuifood_16110100801.iface.UserIface;
import cn.dnui_yxk801.dnuifood_16110100801.listener.OnRetrofitListener;
import cn.dnui_yxk801.dnuifood_16110100801.service.UserService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class UserModel extends BaseModel implements UserIface {
    private UserService service;

    public UserModel(){
        service=retrofit.create(UserService.class);
    }
    @Override
    public void login(String username, String userpass, final OnRetrofitListener listener) {
        //service=retrofit.create(UserService.class);
        Call<UserBean> call=service.doLogin(username,userpass);
       // bindCallBack(call,listener,1);
        call.enqueue(new Callback<UserBean>(){
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                if("0".equals(response.body().getUserid()))
                    listener.onFalure("解析错误!");

                else
                    listener.onSuccess(response.body(), 1);
            }

            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {
                listener.onFalure(t.toString());
            }
        });
    }

    @Override
    public void register(String username, String userpass, String mobilenum, String address, String comment, final OnRetrofitListener listener) {
        //service=retrofit.create(UserService.class);
        Call call;
        call=service.doRegist(username,userpass,mobilenum,address,comment);
        bindCallBack(call,listener,1);

    }
    public void userCollectFood(String userid,String foodid,final OnRetrofitListener listener){
        //service=retrofit.create(UserService.class);
        Call call=service.userCollectFood(userid,foodid);
        Callback<ResultBean> callback = new Callback<ResultBean>() {
            @Override
            public void onResponse(Call<ResultBean> call, Response<ResultBean> response) {
                listener.onSuccess(response.body(), 4);
            }

            @Override
            public void onFailure(Call<ResultBean> call, Throwable t) {
                listener.onFalure(t.toString());
            }
        };
        call.enqueue(callback);

    }
    public void userCollectShop(String userid,String shopid,final OnRetrofitListener listener){
        //service=retrofit.create(UserService.class);
        Call call=service.userCollectShop(userid,shopid);
        Callback<ResultBean> callback = new Callback<ResultBean>() {
            @Override
            public void onResponse(Call<ResultBean> call, Response<ResultBean> response) {
                listener.onSuccess(response.body(), 2);
            }

            @Override
            public void onFailure(Call<ResultBean> call, Throwable t) {
                listener.onFalure(t.toString());
            }
        };
        call.enqueue(callback);

    }

    @Override
    public void getAllUserCollection(String user_id, String flag,final OnRetrofitListener listener) {
        //service=retrofit.create(UserService.class);
        Call call=service.getAllUserCollection(user_id,flag);
  //      bindCallBack(call,listener,6);
        Callback<List> callback = new Callback<List>() {
            @Override
            public void onResponse(Call<List> call, Response<List> response) {
                listener.onSuccess(response.body(), 6);
            }

            @Override
            public void onFailure(Call<List> call, Throwable t) {
                listener.onFalure(t.toString());
            }
        };
        call.enqueue(callback);
    }

    @Override
    public void getUserById(String userid, OnRetrofitListener listener) {
        //service=retrofit.create(UserService.class);
        Call call=service.getUserById(userid);
        bindCallBack(call,listener,9);
    }

    @Override
    public void updateUserById(String userid, String username, String userpass, String mobilenum, String address, OnRetrofitListener listener) {
        Call call=service.updateUserById(userid,username,userpass,mobilenum,address);
        bindCallBack(call,listener,8);
    }

    @Override
    public void getAllOrdersByUser(String userid, OnRetrofitListener listener) {
        Call call=service.getAllOrdersByUser(userid);
        bindCallBack(call,listener,7);
    }

    @Override
    public void getAllCommentsByUser(String userid, OnRetrofitListener listener) {
        Call call=service.getAllCommentsByUser(userid);
        bindCallBack(call,listener,5);
    }

    @Override
    public void updateComment(String item_id, String content ,final OnRetrofitListener listener) {
        Call call=service.updateComment(item_id,content);
        bindCallBack(call,listener,3);
//        Callback<ResultBean> callback = new Callback<ResultBean>() {
//            @Override
//            public void onResponse(Call<ResultBean> call, Response<ResultBean> response) {
//                listener.onSuccess(response.body(), 3);
//            }
//
//            @Override
//            public void onFailure(Call<ResultBean> call, Throwable t) {
//                listener.onFalure(t.toString());
//            }
//        };
//        call.enqueue(callback);
    }

    @Override
    public void insertComment(String item_id, String content, final OnRetrofitListener listener) {
        Call call=service.insertComment(item_id,content);
        //bindCallBack(call,listener,11);
     Callback<ResultBean> callback = new Callback<ResultBean>() {
           @Override
           public void onResponse(Call<ResultBean> call, Response<ResultBean> response) {
               listener.onSuccess(response.body(), 11);
           }

           @Override
           public void onFailure(Call<ResultBean> call, Throwable t) {
               listener.onFalure(t.toString());
           }
       };
       call.enqueue(callback);
    }

    @Override
    public void deleteComment(String item_id, OnRetrofitListener listener) {
        Call call=service.deleteComment(item_id);
        bindCallBack(call,listener,10);
    }
}
